﻿Public Class frm_talles
    Dim check As New Validar
    Dim idProd As Integer
    Dim idTipoTalle As String
    Dim accesoDatosTalle As New AccesoDatos("Talle")
    Dim accesoDatosTipoTalle As New AccesoDatos("TipoTalle")
    

    Private Sub frm_talles_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim nombreTalle As String
        'cmb_talle.Enabled = False


        dgv_talle.Rows.Clear()

        Dim tabla As New Data.DataTable
        tabla = accesoDatosTalle.consulta("SELECT idTalle FROM Talle_Producto where idProducto = " & idProd)
        If tabla.Rows.Count = 0 Then
            cmb_mostrarTipoTalle.Enabled = True
            cargarCombo(cmb_mostrarTipoTalle, accesoDatosTalle.leerTabla("TipoTalle"), "idTipoTalle", "nombre")
            cmb_mostrarTipoTalle.SelectedIndex = 0
        Else
            cmb_mostrarTipoTalle.Enabled = False
            cargarCombo(cmb_mostrarTipoTalle, accesoDatosTipoTalle.leerTabla("TipoTalle"), "idTipoTalle", "nombre")
            idTipoTalle = accesoDatosTalle.consulta("SELECT TipoTalle.idTipoTalle FROM Producto INNER JOIN Talle_Producto ON Producto.idProducto = Talle_Producto.idProducto INNER JOIN Talle ON Talle_Producto.idTalle = Talle.idTalle INNER JOIN TipoTalle ON Talle.idTipoTalle = TipoTalle.idTipoTalle WHERE Producto.idProducto=" & idProd).Rows(0)(0)
            nombreTalle = accesoDatosTalle.consulta("SELECT TipoTalle.nombre FROM Producto INNER JOIN Talle_Producto ON Producto.idProducto = Talle_Producto.idProducto INNER JOIN Talle ON Talle_Producto.idTalle = Talle.idTalle INNER JOIN TipoTalle ON Talle.idTipoTalle = TipoTalle.idTipoTalle WHERE Producto.idProducto=" & idProd).Rows(0)(0)
            cmb_mostrarTipoTalle.SelectedValue = idTipoTalle
            Dim str As String = "SELECT Talle.* FROM TipoTalle INNER JOIN Talle ON TipoTalle.idTipoTalle = Talle.idTipoTalle WHERE TipoTalle.nombre= '" & cmb_mostrarTipoTalle.Text & "'"
            cargarCombo(cmb_talle, accesoDatosTalle.consulta(str), "idTalle", "nombre")
            cmb_talle.Enabled = True
        End If


    End Sub

    Public Sub recibirId(ByVal id As Integer)
        idProd = id
    End Sub

    Private Sub cargarCombo(ByRef combo As ComboBox, ByRef tablaACargar As Data.DataTable, ByVal indice As String, ByVal campo As String)
        combo.DataSource = Nothing
        combo.DataSource = tablaACargar
        combo.ValueMember = indice
        combo.DisplayMember = campo
    End Sub



    Private Function validar() As Boolean
        Dim res As Boolean = True
        If cmb_talle.SelectedIndex = -1 Then
            MessageBox.Show("Seleccione un talle.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            cmb_talle.Focus()
            res = False
        ElseIf check.validarBlanco(txt_cant.Text) = True Then
            MessageBox.Show("Ingrese una cantidad.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            txt_cant.Focus()
            res = False
     
        End If
        Return res
    End Function

    Private Function validarExistenciaGrilla()
        Dim res As Boolean = False
        For i As Integer = 0 To dgv_talle.RowCount - 1
            If cmb_talle.Text = dgv_talle.Rows(i).Cells(0).Value Then
                res = True
            End If
        Next
        Return res
    End Function

    Private Sub cmd_agregar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd_agregar.Click

        If validar() = True Then
            If validarExistenciaGrilla() Then
                MessageBox.Show("El talle que desea ingresar ya ha sido cargado", "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop)
                dgv_talle.DataSource = Nothing
                cmb_talle.SelectedIndex = -1
                txt_cant.Text = ""
            Else
                If IsNumeric(txt_cant.Text) Then

                    Dim talle As String
                    Dim cant As Integer

                    talle = cmb_talle.Text
                    cant = txt_cant.Text
                    dgv_talle.Rows.Add(talle, cant, cmb_talle.SelectedValue)
                    cmb_talle.SelectedIndex = -1
                    txt_cant.Text = ""
                    cmb_mostrarTipoTalle.Enabled = False
                Else
                    txt_cant.Text = ""
                    txt_cant.Focus()
                End If
            End If
        End If

    End Sub

    Private Sub cmd_confirmar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd_confirmar.Click
        frm_compra.cargarCompra(dgv_talle)
        frm_compra.limpiarFormCompra()
    End Sub

    Private Sub cmb_talle_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmb_talle.SelectedIndexChanged
        If cmb_mostrarTipoTalle.SelectedIndex = -1 Then
            MessageBox.Show("Selecciones el tipo de talle del producto", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            cmb_mostrarTipoTalle.Focus()
        End If
    End Sub

    Private Sub cmb_mostrarTipoTalle_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmb_mostrarTipoTalle.SelectedIndexChanged
        If cmb_mostrarTipoTalle.SelectedIndex <> -1 Then
            Dim str As String = "SELECT Talle.* FROM TipoTalle INNER JOIN Talle ON TipoTalle.idTipoTalle = Talle.idTipoTalle WHERE TipoTalle.nombre= '" & cmb_mostrarTipoTalle.Text & "'"
            cmb_talle.Enabled = True
            cargarCombo(cmb_talle, accesoDatosTalle.consulta(str), "idTalle", "nombre")
        End If
    End Sub

    Private Sub cmd_salir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd_salir.Click
        Me.Close()
    End Sub
End Class